// eduNews/educationTable/educationTable.js

const app=getApp();
var util=require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsArray:[],
    titleArray:[],
    selectId:'',
    pageNo:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'news/list',
      data:{
        pageNo:1,
        pageSize:10
      },
      method:'get',
      success:function(res){
        console.log(res)
        var resData=res.data.data.dataList.data
       var index
       for(index=0;index<resData.length;index++){
         var img='newsArray['+index+'].img'
         var title = 'newsArray[' + index + '].title'
         var content = 'newsArray[' + index + '].content'
         var id='newsArray['+index+'].id'
         var createTime='newsArray['+index+'].time'
         var commit='newsArray['+index+'].commit'
         var time=resData[index].gmtModifiedFormat
         var read='newsArray['+index+'].read'
      // var createTime=time.getFullYear()
         time=time.substring(0,10)
         var t=resData[index].title
         if(t.length>18){
           t=t.substring(0,16)+'...'
         }else{
           t=t
         }
       
         that.setData({
           [img]:resData[index].cover_img,
           [title]: t,
           [content]: resData[index].cover_description,
           [createTime]:time,
           [commit]:resData[index].commentTotal,
           [id]:resData[index].id,
           [read]:resData[index].read
          })         
       }
      }
    }) 
   // app.qingqiu('news/type','','',this.callbackFun)
      wx.request({
        url: app.globalData.baseUrl+'news/type',
        header:{
          'cookie':'JSESSION='+wx.getStorageSync('sessionID')
        },
        method:'GET',
        success:function(res){
           var resData=res.data.data.data
           for(var i=0;i<resData.length;i++){
            var id='titleArray['+i+'].id'
            var title='titleArray['+i+'].title'
            if(resData.name!=''){
              that.setData({
                [id]:resData[i].id,
                [title]:resData[i].name
              })
             
            }
           }
        }
      })
  },
 
   choice:function(response)
   {  
     this.setData({
       newsArray:[],
     })
     var that=this
     console.log(response)
     var id=response.detail.id
     that.setData({
       selectId:id
     })
     wx.request({
       url: app.globalData.baseUrl+'news/list/'+id,
       method:'get',
       header:{
         'cookie':wx.getStorageSync('sessionID')
       },
       success:function(res){
         console.log(res)
         var resData=res.data.data.data
         if(res.data.data.data.length!=0){
           that.setData({
             newsArray:[],
           })
           for (var index = 0; index < resData.length; index++) {
             var img = 'newsArray[' + index + '].img'
             var title = 'newsArray[' + index + '].title'
             var content = 'newsArray[' + index + '].content'
             var id = 'newsArray[' + index + '].id'
             var createTime = 'newsArray[' + index + '].time'
             var commit = 'newsArray[' + index + '].commit'
             var time = resData[index].gmtCreate
             var read = 'newsArray[' + index + '].read'
             //  var createTime=time.getFullYear()
             // time=time.substring(0,10)
             var t = resData[index].title
             if (t.length > 18) {
               t = t.substring(0, 16) + '...'
             } else {
               t = t
             }
             that.setData({
              
               [img]: resData[index].cover_img,
               [title]: t,
               [content]: resData[index].cover_description,
               [createTime]:resData[index].gmtModifiedFormat.substring(0,10),
               [commit]: resData[index].commentTotal,
               [id]: resData[index].id,
               [read]: resData[index].read
             })
           }
         }
       }

     })
     
   }, 
   toContent:function(event){
     console.log(event)
     var id=event.currentTarget.dataset.id
     wx.navigateTo({
       url: '../newsContent/newsContent?id='+id
     })
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // for(var i=0;i<100;i++){
    //   app.qingqiu('news/insert', { type_id: 1000, admin_id:1000, title: '201' + i + '年发生的大事',description:'201aaa年是暂新的一年'}, 'post', this.callbackFun)
    // }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  getMoreEducationNews:function(pageNo){
    var selectId=this.data.selectId
    var url
    if(selectId==''){
      url=app.globalData.baseUrl+'news/list'
    }else{
      url = app.globalData.baseUrl + 'news/list'+id
    }
    wx.request({
      url: url,
      data: {
        pageNo: pageNo,
        pageSize: 10
      },
      method: 'get',
      success: function (res) {
        console.log(res)
        try{
          var resData = res.data.data.dataList.data
          var index
          for (index = pageNo * 10; index < pageNo * 10 + resData.length; index++) {
            var img = 'newsArray[' + index + '].img'
            var title = 'newsArray[' + index + '].title'
            var content = 'newsArray[' + index + '].content'
            var id = 'newsArray[' + index + '].id'
            var createTime = 'newsArray[' + index + '].time'
            var commit = 'newsArray[' + index + '].commit'
            var time = resData[index % 10].gmtModifiedFormat
            var read = 'newsArray[' + index + '].read'
            // var createTime=time.getFullYear()
            time = time.substring(0, 10)
            var t = resData[index % 10].title
            if (t.length > 18) {
              t = t.substring(0, 16) + '...'
            } else {
              t = t
            }

            that.setData({
              [img]: resData[index % 10].cover_img,
              [title]: t,
              [content]: resData[index % 10].cover_description,
              [createTime]: time,
              [commit]: resData[index % 10].commentTotal,
              [id]: resData[index % 10].id,
              [read]: resData[index % 10].read
            })
          }
        }catch(err){
          wx.showToast({
            title: '暂时没有更多头条新闻哦~~~',
            icon: 'none'
          })
        }
       
      }
    }) 
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNo = this.data.pageNo
    console.log('下拉触底')
    try{
      this.getMoreEducationNews(pageNo)
      this.setData({
        pageNo: pageNo + 1
      })
    }catch(err){
      console.log(err)
      wx.showToast({
        title: '暂时没有更多头条新闻哦~~~',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})